package org.testis.testdriver;

import org.testis.testdriver.AtofEnumaration.AtofLogLevel;
import java.util.Vector;


/**
 *
 * @author barisergun
 */
public class NotificationSubjectSingleton
{

    private static NotificationSubjectSingleton m_subject;
    protected Vector<InputNotificationInterface> m_listeners;
    private boolean m_consoleInputNeeded;
    private AtofEnumaration.ExecutionInterventionType m_previousInterventType;


    private NotificationSubjectSingleton(boolean consoleInput)
    {
        m_listeners = new Vector<InputNotificationInterface>();
        m_consoleInputNeeded = consoleInput;

    }


    /**
     * A static method for creating singleton instance of ExecutionGuidProvider
     * @return
     */
    public static NotificationSubjectSingleton createInstance(
        boolean consoleInput)
    {
        if (m_subject == null) {
            m_subject = new NotificationSubjectSingleton(consoleInput);
        }

        return m_subject;
    }


    public static NotificationSubjectSingleton getInstance()
    {
        return m_subject;
    }


    public void register(InputNotificationInterface input)
    {
        this.m_listeners.addElement(input);
    }


    public void unregister(InputNotificationInterface input)
    {
        this.m_listeners.removeElement(input);
    }


    public void notifyUsers()
    {
        for (java.util.Enumeration e = m_listeners.elements();
            e.hasMoreElements();) {
            ((InputNotificationInterface) e.nextElement()).needInput();
        }

    }

    public void notifyExecutionStatusToUsers(
        AtofEnumaration.ExecutionInterventionType val)
    {
        boolean informStateChange = false;

        if (m_previousInterventType != null) {
            if (val != m_previousInterventType) {
                informStateChange = true;
            }
        }
        else {
            informStateChange = true;
        }
        if (informStateChange) {
            m_previousInterventType = val;
            for (java.util.Enumeration e = m_listeners.elements();
                e.hasMoreElements();) {
                ((InputNotificationInterface) e.nextElement()).
                    execInterventionStateChanged(val);
            }
        }
    }

    public void notifyLogHeaderMessages(int designTreeId,
            int designPartType,
            Byte result,
            String parentGuid,
            String name,
            String guid,
            String executionGuid){

        for (java.util.Enumeration e = m_listeners.elements();
            e.hasMoreElements();) {
            ((InputNotificationInterface) e.nextElement()).notifyLogHeaderMessages(
                    designTreeId,
                    designPartType,
                    result,
                    parentGuid,
                    name,
                    guid,
                    executionGuid);
        }
    }

    public void notifyLogMessages(String objectDesc,
            AtofLogLevel level,
            String logMsg,
            String guid){
            for (java.util.Enumeration e = m_listeners.elements();
            e.hasMoreElements();) {
            ((InputNotificationInterface) e.nextElement()).notifyLogMessages(
                    objectDesc,
                    level,
                    logMsg,
                    guid);
        }
    }

    public boolean isConsoleInputNeeded()
    {
        return m_consoleInputNeeded;
    }
}
